/**
 * 
 */
package cn.nawang.ebeim.api.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.nawang.ebeim.api.comm.BaseController;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.UserBeHaviorsLog;
import cn.nawang.ebeim.api.service.UserBehaviorsLogService;
import cn.nawang.ebeim.api.utils.Constant;


/**
 * @ClassName: UserBehavitorController
 * @Description: TODO(用户行为日志控制器)
 * @author 阮志忠
 * @date 2015年11月17日 上午11:46:30
 * 
 */
@RestController
public class UserBehavitorsLogController extends BaseController{
    private static final Logger LOG = LoggerFactory.getLogger("user.behaviors");
    @Autowired
    private UserBehaviorsLogService userBehaviorsLogService;


    /**
     * @Title: uploadByBackupLog @Description: TODO(上传用户行为日志) @param @return Map
     *         <String,Object> @throws
     */
    @RequestMapping(value = "/userBeHaviorsLog/upload", method = {RequestMethod.POST})
    public Map<String, Object> uploadByBackupLog(@RequestParam("file") MultipartFile file,
            HttpServletRequest request, ModelMap model) {

        String path = request.getSession().getServletContext().getRealPath("upload");
        if (MyTaskAnnotation.uploadPath != null) {
            path = MyTaskAnnotation.uploadPath;
        }
        
        String userID = request.getParameter("userID");

        String fileName = UUID.randomUUID().toString().replace("-", "")+"-"+userID+".bhs";
        Map<String, Object> map = new HashMap<String, Object>();
        File targetFile = new File(path, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }

        // 保存
        try {
            file.transferTo(targetFile);
            map.put(Constant.JSON_CODE, Constant.CODE_200);
        } catch (Exception e) {
            e.printStackTrace();
            map.put(Constant.JSON_CODE, Constant.CODE_ERROR);
        }


        return map;
    }


    
    @RequestMapping(value = "/userBeHaviorsLog/findByBackupLog", method = {RequestMethod.POST,
            RequestMethod.GET})
    public PageList<UserBeHaviorsLog> findByBackupLog(HttpServletRequest request,Pages page) {
        UserBeHaviorsLog userBeHaviorsLog = new UserBeHaviorsLog();
        userBeHaviorsLog.setUserID(getUserLoginInfo(request).getId());
        PageList<UserBeHaviorsLog> userBeHaviorsLogsList =
                userBehaviorsLogService.findByPage(userBeHaviorsLog,page);

        return userBeHaviorsLogsList;
    }


    @Deprecated
    @RequestMapping(value = "/userBeHaviorsLog/delete", method = {RequestMethod.GET})
    public Map<String, Object> delete() {
        Map<String, Object> map = new HashMap<String, Object>();
        userBehaviorsLogService.deleteExpireBackupLog(new Date(2015,11,25,8,38,01));
        map.put(Constant.JSON_CODE, Constant.CODE_200);
        return map;
    }
    
    @Deprecated
    @RequestMapping(value = "/userBeHaviorsLog/add", method = {RequestMethod.POST})
    public Map<String, Object> add(UserBeHaviorsLog userBeHaviorsLog) {
        Map<String, Object> map = new HashMap<String, Object>();
        userBehaviorsLogService.save(userBeHaviorsLog);
        map.put(Constant.JSON_CODE, Constant.CODE_200);
        return map;
    }

}
